package com.greateamd.timetableasy.client.widgets;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.uibinder.client.UiConstructor;
import com.greateamd.timetableasy.domain.PersistableEntity;

public class EntityContainer<T extends PersistableEntity> extends ListBox {

	List<T> entities = new ArrayList<T>();
	
	public @UiConstructor EntityContainer(boolean multi) {
		super(multi);
	}
	
	public boolean addEntity(T t){
		final String search = getSignificatifString(t);
		if(!entities.contains(search)){
			entities.add(t);
			addItem(search);
			return true;
		}
		return false;
	}
	
	public T getSelectedEntity(){
		if(getSelectedIndex()!= -1){
			return entities.get(getSelectedIndex());
		}
		return null;
	}
	
	@Override
	public void clear() {
		entities.clear();
		super.clear();
	}
	
	public void removeSelectedEntity(){
		if(getSelectedIndex()!= -1){
			entities.remove(getSelectedIndex());
			removeItem(getSelectedIndex());
		}
	}
	
	public List<T> getEntities(){
		return entities;
	}
	
	public String getSignificatifString(T t){
		return t.toString();
	}
}
